package etr.bk.gui.rpan.image;

import etr.bk.db.WrapperFotoPan;
import etr.bk.db.WrapperLPan;
import etr.bk.gui.*;
import etr.bk.gui.lpan.PictPan;
import etr.bk.gui.rpan.image.ImageFotoPan;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.h2.jdbc.JdbcSQLException;

public class PreviewImagePanel extends JPanel implements MouseListener {
	private JScrollPane scrolpanel = null;
	private JList<ImageFotoPan> itemList = null;
	DefaultListModel<ImageFotoPan> defListMod = null;

	private JButton butAdd = null;
	private JButton butDel = null;

	private JProgressBar progressBar = null;

	private MainListener mListener;

	private int currentSelectID;
	
	private int [] forDel;

	public PreviewImagePanel() {
		super();

		this.mListener = MainListener.getListener();
		this.mListener.setPreviewImagePanel(this);

		defListMod = new DefaultListModel<ImageFotoPan>();

		this.itemList = new JList<ImageFotoPan>(defListMod);
		this.itemList.setCellRenderer(new RenderFotoPan());
		this.itemList.addListSelectionListener(new ListSelListen());
		this.itemList.addMouseListener(this);

		itemList.setVisibleRowCount(200);
		//itemList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		itemList.setLayoutOrientation(itemList.HORIZONTAL_WRAP);
		itemList.setVisibleRowCount(-1);
		itemList.setBackground(Color.LIGHT_GRAY);

		this.scrolpanel = new JScrollPane(itemList);
		this.scrolpanel.setPreferredSize(new Dimension(550, 400));
		this.scrolpanel
				.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
		this.scrolpanel
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		this.scrolpanel.setAlignmentX(CENTER_ALIGNMENT);
		this.scrolpanel.setBorder(BorderFactory.createLoweredBevelBorder());
		this.add(scrolpanel);

		// ----------------------

		this.butAdd = new JButton("Add");
		this.butDel = new JButton("Dell");

		butAdd.addActionListener(mListener);
		butDel.addActionListener(mListener);

		this.add(butAdd);
		this.add(butDel);

		progressBar = new JProgressBar();
		progressBar.setPreferredSize(new Dimension(500, 20));
		progressBar.setMinimum(0);

		this.add(progressBar);

		// progressBar.setVisible(false);

		this.mListener.setProgressBar(this.progressBar);
		this.mListener.setImageADD(butAdd);
		this.mListener.setImageDEL(butDel);

		this.currentSelectID = -1;
	}

	public void setItemList(ArrayList<WrapperFotoPan> arrList) {
		ImageFotoPan tmpPan = null;
		this.defListMod.removeAllElements();
		if (arrList != null && !arrList.isEmpty()) {
			for (WrapperFotoPan tmp : arrList) {

				tmpPan = new ImageFotoPan(tmp.getImg(), tmp.getId());
				tmpPan.addMouseListener(this);
				if (tmpPan != null)
					this.defListMod.addElement(tmpPan);
				System.out.println("add");
				//arrList = null;
				//tmpPan = null;
			}
		}
	}

	public void clearItemList() {
		this.defListMod.removeAllElements();
	}

	// -------------Get Current ID-------------------

	public int getCurrentSelectID() {
		return currentSelectID;
	}

	public ArrayList<ImageFotoPan> getSelectedFotos() {
		//int[] indArr = this.itemList.getSelectedIndices();
		this.forDel = this.itemList.getSelectedIndices();
		ArrayList<ImageFotoPan> rez = new ArrayList<ImageFotoPan>();

		for (int i : forDel) {
			rez.add(this.defListMod.getElementAt(i));
		}
		if (rez.isEmpty())
			return null;
		
		return rez;
	}
	
	public void deleteItems(boolean yes){
		if (yes && forDel.length != 0){
			System.out.println(forDel.length);
			
			for(int i = forDel.length-1; i>=0; i--){
				this.defListMod.removeElementAt(forDel[i]);
			}
		}
		
		this.forDel = null;
	}

	// --------Inner listener-----------------
	private class ListSelListen implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent arg0) {
			if (arg0.getValueIsAdjusting()) {
				ImageFotoPan pan = null;
				pan = PreviewImagePanel.this.itemList.getSelectedValue();
				PreviewImagePanel.this.currentSelectID = pan.getID();
				
				//System.out.println("|||||||||" + pan.getID() + " "
					//	+ PreviewImagePanel.this.defListMod.indexOf(pan));

				// FotoPanel.this.defListMod.
			}
		}
	}

	// ====================================================================

	public void mouseClicked(MouseEvent e) {

		if (e.getClickCount() == 2) {
			// assign into listener ID of foto
			System.out.println("Double click");
			ImageFotoPan tmpPan = this.itemList.getSelectedValue();// (FotoPan)
																	// e.getSource();
			mListener.setFotoID(tmpPan.getID());
			mListener.setFotoIndex(defListMod.indexOf(tmpPan));
			System.out.println("****** FotoID "+tmpPan.getID()+" FotoIndex "+defListMod.indexOf(tmpPan));
			
			mListener.setArrayOfImage(PreviewImagePanel.this.defListMod.toArray());
			this.mListener.mouseClicked(new MouseEvent(this, e.getID(), e
					.getWhen(), e.getModifiers(), e.getX(), e.getY(), e
					.getClickCount(), e.isPopupTrigger()));
		}
	}

	// ----------Interfaces remains --------------------------
	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent arg0) {
	}

	public void mouseReleased(MouseEvent arg0) {
	}

	// ----------------------------------------------------------------------------
}
